package io.babystar.modules.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.R;
import io.babystar.common.utils.ShiroUtils;
import io.babystar.modules.business.entity.CWCategoryEntity;
import io.babystar.modules.business.service.CWCategoryService;
import io.babystar.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("business/cwCategory")
public class CWCategoryController {
    @Autowired
    private CWCategoryService cwCategoryService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("business:cwCategory:list")
    public List<CWCategoryEntity> list(@RequestParam Map<String, Object> params){
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        HashMap<String,Object> par = new HashMap<>();
        par.put("business_id",userEntity.getBusinessId());
        List<CWCategoryEntity> cwCategoryEntityList = cwCategoryService.listByMap(par);
        for (CWCategoryEntity cwCategoryEntity : cwCategoryEntityList) {
            CWCategoryEntity categoryEntity = cwCategoryService.getById(cwCategoryEntity.getPid());
            if(null != categoryEntity){
                cwCategoryEntity.setParentCategory(categoryEntity.getCategory());
            }
        }
        return cwCategoryEntityList;
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("business:cwCategory:info")
    public R info(@PathVariable("id") Long id){
        CWCategoryEntity cwCategory = cwCategoryService.getById(id);

        return R.ok().put("cwCategory", cwCategory);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("business:cwCategory:save")
    public R save(@RequestBody CWCategoryEntity cwCategory){
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        List<CWCategoryEntity> cwCategoryList = cwCategoryService.list(
                new QueryWrapper<CWCategoryEntity>()
                .eq("category", cwCategory.getCategory())
                .eq("business_id",userEntity.getBusinessId()));
        if(cwCategoryList.size() > 0){
            return R.error(1,"已存在该分类");
        }
        cwCategory.setBusiness_id(Integer.parseInt(userEntity.getBusinessId()+""));
        cwCategoryService.save(cwCategory);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @RequiresPermissions("business:cwCategory:update")
    public R update(@RequestBody CWCategoryEntity cwCategory){
        cwCategoryService.updateById(cwCategory);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresPermissions("business:cwCategory:delete")
    public R delete(@RequestBody Integer[] ids){
        for (Integer id : ids) {
            //判断是否有子类
            List<Integer> list = cwCategoryService.queryPidList(id);
            if(list.size() > 0){
                return R.error("请先删除子类");
            }
        }
        cwCategoryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
